import { PaginatorPlugin } from "@datorama/akita";
import { switchMap } from "rxjs/operators";

import authService from "@/services/authService";
import { RoomsQuery, RoomsStore, makeStore, RoomEntity } from "@/state/fansEntity/store";

export class RoomsFacade {
  constructor(private store: RoomsStore, private query: RoomsQuery, public paginator: PaginatorPlugin<RoomEntity>) {

  }
  readonly filter$ = this.query.filter$;
  pagination$ = this.paginator.pageChanges.pipe(
    //获取关注的粉丝列表数据 
    switchMap((pageNum) => {
      const reqFn = () => authService.getFansSvc(pageNum, 10);
      return this.paginator.getPage(reqFn);
    })
  )

  prevPage = () => {
    this.paginator.prevPage();
  }

  nextPage = () => {
    this.paginator.nextPage();
  }

  //销毁store实例，释放内存
  destroy = () => {
    this.paginator.destroy();
    this.store.destroy();
  }
}

export const makeFacade = (): RoomsFacade => {
  const [store, query, rooms_paginator] = makeStore();

  return new RoomsFacade(store, query, rooms_paginator);
}